package com.eims.sls_app.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import bls.juejin.cn.R;
import com.eims.sls_app.dataaccess.Schemes;
import com.eims.sls_app.utils.Arith;
import com.eims.sls_app.utils.LotteryUtils;
import com.eims.sls_app.view.MyListView2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

public class CenterLotteryAdapter extends BaseAdapter {

	private Context mContext;
	private HashMap<String, List<String>> mMap;
	private List<String> dath;
	private List<List<Schemes>> mList;
	private OnItemClickListener listener;
	private int type = 1; // 1显示彩种名字 2显示期号

	public CenterLotteryAdapter(Context context, List<String> list, List<List<Schemes>> mlist,
			OnItemClickListener listener, int type) {
		this.mContext = context;
		this.listener = listener;
		this.type = type;
		setDate(list, mlist);
	}

	public CenterLotteryAdapter(Context context, List<String> list, List<List<Schemes>> mlist,
			OnItemClickListener listener) {
		this.mContext = context;
		this.listener = listener;
		setDate(list, mlist);
	}

	public void setDate(List<String> list, List<List<Schemes>> mlist) {
		if (null == dath)
			dath = new ArrayList<String>();
		if (null == mList)
			mList = new ArrayList<List<Schemes>>();
		dath.clear();
		mList.clear();
		for (String str : list) {
			dath.add(str);
		}
		for (List<Schemes> li : mlist) {
			List<Schemes> l = new ArrayList<Schemes>();
			for (Schemes str : li) {
				l.add(str);
			}
			mList.add(l);
		}
	}

	/** 给集合赋值 */
	public void setHashMap(HashMap<String, List<String>> set) {
		for (Entry<String, List<String>> entry : set.entrySet()) {
			List<String> l = new ArrayList<String>();
			for (String s : entry.getValue()) {
				l.add(s);
			}
			this.mMap.put(entry.getKey(), l);
		}
	}

	@Override
	public int getCount() {
		return dath.size();
	}

	@Override
	public Object getItem(int arg0) {
		return dath.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		ViewHolder holder;
		if (null == view) {
			LayoutInflater inflact = LayoutInflater.from(mContext);
			view = inflact.inflate(R.layout.center_lottery_item, null);
			holder = new ViewHolder();
			holder.tv_month = (TextView) view.findViewById(R.id.tv_month);
			holder.tv_day = (TextView) view.findViewById(R.id.tv_day);
			holder.listView = (MyListView2) view.findViewById(R.id.item_listView);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		holder.listView.setAdapter(new MyListViewAdapter(mList.get(position), position));
		holder.listView.setOnItemClickListener(listener);
		holder.tv_month.setText(dath.get(position).split("-")[1] + "月");
		holder.tv_day.setText(dath.get(position).split("-")[2]);
		return view;
	}

	static class ViewHolder {
		TextView tv_month, tv_day;
		MyListView2 listView;
	}

	class MyListViewAdapter extends BaseAdapter {
		private List<Schemes> aList;
		private int id;

		public MyListViewAdapter(List<Schemes> aList, int id) {
			this.aList = aList;
			this.id = id;
		}

		@Override
		public int getCount() {
			return aList.size();
		}

		@Override
		public Object getItem(int position) {
			return aList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder2 holder;
			if (null == convertView) {
				LayoutInflater inflact = LayoutInflater.from(mContext);
				convertView = inflact.inflate(R.layout.center_lottery_item_item, null);
				holder = new ViewHolder2();
				holder.tv_name = (TextView) convertView.findViewById(R.id.tv_lotteryName);
				holder.iconOptimize = convertView.findViewById(R.id.optimize);
				holder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
				holder.tv_type = (TextView) convertView.findViewById(R.id.tv_playType);
				holder.tv_isWin1 = (TextView) convertView.findViewById(R.id.tv_isWin1);
				holder.tv_win = (TextView) convertView.findViewById(R.id.tv_isWin);
				holder.tv_id = (TextView) convertView.findViewById(R.id.tv_id);
				holder.img_logo = (ImageView) convertView.findViewById(R.id.img_win);
				holder.img_title = (ImageView) convertView.findViewById(R.id.title_type);
				holder.tvCaiJin = (TextView) convertView.findViewById(R.id.tv_CaiJinCost);
				holder.tvJinE = (TextView) convertView.findViewById(R.id.tv_JinECost);
				holder.tvHongbaoShow = (TextView) convertView.findViewById(R.id.hongbao_show);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder2) convertView.getTag();
			}

			holder.tv_id.setText(id + "");
			holder.tv_id.setVisibility(View.GONE);
			holder.img_logo.setVisibility(View.GONE);

			Schemes scheme = aList.get(position);

			
			if (type == 2)

				holder.tv_name.setText("第" + scheme.getIsuseName() + "期");
			else {
				String getName = LotteryUtils.getTitleText(scheme.getLotteryID());
				holder.tv_name.setText(getName);

				if (getName.equals("竞彩足球")) {
					holder.img_title.setBackgroundResource(R.drawable.bet_loty_logo_jczq_main);
				} else if (getName.equals("竞彩篮球")) {
					holder.img_title.setBackgroundResource(R.drawable.bet_loty_logo_jclq_main);
				} else if (getName.equals("大乐透")) {
					holder.img_title.setBackgroundResource(R.drawable.bet_loty_logo_dlt_main);
				} else if (getName.equals("胜负彩")) {
					holder.img_title.setBackgroundResource(R.drawable.bet_loty_logo_sfc_main);
				} else if (getName.equals("任九场")) {
					holder.img_title.setBackgroundResource(R.drawable.bet_loty_logo_r9_main);
				} else if (getName.equals("北京单场")) {
					holder.img_title.setBackgroundResource(R.drawable.logo_sfc);
				} else {
					holder.img_title.setBackgroundResource(R.drawable.bet_loty_logo_jczqdg_main);
				}
			}
			holder.iconOptimize.setVisibility(scheme.getIsPreBet() ? View.VISIBLE : View.GONE);
			holder.tvCaiJin.setText("投注时间：" + scheme.getDateTime());
			if (scheme.getHb() != null) {
				if (scheme.getHb().equals("0")) {
					holder.tvHongbaoShow.setVisibility(View.GONE);
				} else {
					holder.tvHongbaoShow.setVisibility(View.VISIBLE);
				}
			} else {
				holder.tvHongbaoShow.setVisibility(View.GONE);
			}

			//
			Double sumMoney = Double.parseDouble(scheme.getDetailMoney())
					+ Double.parseDouble(scheme.getHandselMoney());
			holder.tvJinE.setText("投注金额：" + sumMoney + "元");

			holder.tv_money.setText(scheme.getMoney() + "元");
			holder.tv_win.setTextColor(Color.BLACK);
			String status = scheme.getSchemeStatus();
			Log.e("-----", "getView: "+scheme.getDescription() );
			if (status.equals("已中奖")) {
				holder.tv_win.setTextColor(Color.RED);
				holder.img_logo.setVisibility(View.VISIBLE);

				if (scheme.getDescription() != null && !scheme.getDescription().equals("")) {
					holder.tv_isWin1.setTextColor(Color.RED);
					String[] split = scheme.getDescription().split("加奖juejin310");

					if (split.length > 1) {
						double money_zhongjian = Arith.sub(scheme.getWinMoneyNoWithTax(), Double.parseDouble(split[1]));
						holder.tv_isWin1.setVisibility(View.VISIBLE);
						holder.tv_win.setText("中奖" + money_zhongjian + "元");
						if (split[1].equals("0")) {
							holder.tv_isWin1.setVisibility(View.GONE);
							holder.tv_isWin1.setText("");
						} else {
							holder.tv_isWin1.setVisibility(View.VISIBLE);
							holder.tv_isWin1.setText("加奖:" + split[1]);
						}

					} else {
						holder.tv_win.setText("中奖" + scheme.getWinMoneyNoWithTax() + "元");
					}

				} else {
					holder.tv_win.setText("中奖" + scheme.getWinMoneyNoWithTax() + "元");
				}

			} else {
				holder.tv_isWin1.setVisibility(View.GONE);
				holder.tv_win.setText(status);
			}
			// Log.i("scheme", "scheme.getIsfollow()=="+scheme.getIsfollow());

			// holder.tv_type.setText("代购");
			if ("False".equals(scheme.getIsPurchasing())) {
				holder.tv_type.setText("合买订单");
			} else if ("True".equals(scheme.getIsPurchasing())) {
				// 普通订单状态
				if (scheme.getIsChase() == 0) {
					holder.tv_type.setText("普通订单");
					// holder.tv_type.setText("代购");
					if ("1".equals(scheme.getIsfollow())) {
						holder.tv_type.setText("我的跟单");
					} else {
						holder.tv_type.setText("代购");
					}
				} else {
					/** 追号订单状态 */
					holder.tv_type.setText("追号订单");
				}
			}
			/*
			 * if (holder.tv_type.getText().equals("普通订单")) {
			 * 
			 * }
			 */

			/*
			 * if (scheme.getIsfollow().equals("1")) { holder.tv_type.setText("跟单"); }else{
			 * holder.tv_type.setText("代购"); }
			 */

			/** 合买订单状态 */
			/*
			 * if ("False".equals(scheme.getIsPurchasing())) {
			 * holder.tv_type.setText("合买订单"); } else if
			 * ("True".equals(scheme.getIsPurchasing())) {
			 *//** 普通订单状态 */
			/*
			 * if (scheme.getIsChase() == 0) { //holder.tv_type.setText("普通订单");
			 * 
			 * } else {
			 *//** 追号订单状态 *//*
							 * holder.tv_type.setText("追号订单"); } }
							 */
			return convertView;
		}
	}

	static class ViewHolder2 {
		TextView tv_name, tv_money, tv_type, tv_win, tv_id, tvJinE, tvCaiJin, tv_isWin1, tvHongbaoShow;
		ImageView img_logo, img_title;
		View iconOptimize;
	}

}
